﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;

namespace OverNANA
{
    public partial class dlgSetting : Form
    {
        public dlgSetting()
        {
            InitializeComponent();
        }

        private void ReadSettings()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            AppSettingsSection appStettings = (AppSettingsSection)config.GetSection("appSettings");
            if (appStettings.Settings["UpdateURL"] == null)
            {
                appStettings.Settings.Add("UpdateURL", "http://www.nanadm.com/dahai/888/");
                tbxUpdateURL.Text = "http://www.nanadm.com/dahai/888/";
                config.Save(System.Configuration.ConfigurationSaveMode.Modified);
            }
            else
            {
                tbxUpdateURL.Text = appStettings.Settings["UpdateURL"].Value;
            }

            if (appStettings.Settings["DownLoadDIR"] == null)
            {
                appStettings.Settings.Add("DownLoadDIR", @"c:\OverDL\");
                tbxDownLoadDirectory.Text = @"c:\OverDL";
                config.Save(System.Configuration.ConfigurationSaveMode.Modified);
            }
            else
            {
                tbxDownLoadDirectory.Text = appStettings.Settings["DownLoadDIR"].Value;
            }

            if (appStettings.Settings["ZIPFile"] == null)
            {
                appStettings.Settings.Add("ZIPFile", @"1");
                cbxZIP.Checked = true;
                config.Save(System.Configuration.ConfigurationSaveMode.Modified);
            }
            else
            {
                if (Convert.ToInt32(appStettings.Settings["ZIPFile"].Value) > 0)
                {
                    cbxZIP.Checked = true;
                }
                else
                {
                    cbxZIP.Checked = false;
                }
            }
        }

        private void WriteSettings()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            AppSettingsSection appStettings = (AppSettingsSection)config.GetSection("appSettings");
            appStettings.Settings["UpdateURL"].Value = tbxUpdateURL.Text;
            if (tbxDownLoadDirectory.Text[tbxDownLoadDirectory.Text.Length - 1] != Convert.ToChar(@"\"))
            {
                tbxDownLoadDirectory.Text = tbxDownLoadDirectory.Text + @"\";
            }
            appStettings.Settings["DownLoadDIR"].Value = tbxDownLoadDirectory.Text;
            config.Save(System.Configuration.ConfigurationSaveMode.Modified);
        }

        private void dlgSetting_Load(object sender, EventArgs e)
        {
            ReadSettings();
        }

        private void btnSubimt_Click(object sender, EventArgs e)
        {
            WriteSettings();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
    }
}
